#!/bin/sh

revision="Car Play activation v0.1.3 (2023-07-22 by MIBonk & MIB-Wiki)"
# use --help for more info

export PATH=:/proc/boot:/sbin:/bin:/usr/bin:/usr/sbin:/net/mmx/bin:/net/mmx/usr/bin:/net/mmx/usr/sbin:/net/mmx/sbin:/net/mmx/mnt/app/armle/bin:/net/mmx/mnt/app/armle/sbin:/net/mmx/mnt/app/armle/usr/bin:/net/mmx/mnt/app/armle/usr/sbin
export LD_LIBRARY_PATH=/net/mmx/mnt/app/root/lib-target:/net/mmx/mnt/eso/lib:/net/mmx/eso/lib:/net/mmx/mnt/app/usr/lib:/net/mmx/mnt/app/armle/lib:/net/mmx/mnt/app/armle/lib/dll:/net/mmx/mnt/app/armle/usr/lib
export IPL_CONFIG_DIR=/etc/eso/production

thisname="$(basename $0)"
thisdir="$(dirname $0)"

if [ -z $LOG ]; then
	. $thisdir/../config/GLOBALS
	echo -ne "\n$ME-$thisname---->\n" >> $LOG
fi

if [ -f $TMP/reboot.mib ]; then
	echo "Reboot running..."
	return 2> /dev/null
fi

if [ "$2" = "-noboot" ]; then
	noboot=$2
else
	noboot=""
fi

MIBCAP=2 #new and old MHI2
. $thisdir/../config/MIBCHECK

	case $1 in

	-a) {

if [[ "$TRAINVERSION" = *MHIG* ]]; then
	echo -ne "!!! Can not be enabled on MHIG\n    Stopping here\n" | $TEE -a $LOG
else
	if [[ "$TRAINVERSION" = *CN* ]]; then
		echo -ne "--- BAIDU CarLife activated now\n" | $TEE -a $LOG
		$PERSW -t blob -P -L 1 -O 57 -f 0 3221356628 F # 7.6 1
	fi
	$PERSW -t blob -P -L 1 -O 56 -f 0 3221356628 F # Android Auto 7.7 1
	$PERSW -t blob -P -L 1 -O 71 -f 0 3221356628 F # CarPlay 8.0 1
	$PERSW -t blob -P -L 1 -O 66 -f 0 3221356628 F # MirrorLink 8.5 1
	$PERSW -t int -f 0 1343769792 3 # USB configuration 3
	[ -z "$GEM" ] && echo -ne "\033[36m"
	echo -ne "--- CarPlay and Android Auto activated now\n" | $TEE -a $LOG
	[ -z "$GEM" ] && echo -ne "\n"
	[ -z "$GEM" ] && echo -ne "\033[m"
	[ -z "$noboot" ] && . $thisdir/reboot -t 10
fi

return 2> /dev/null

	};;

	-s) {

# Only working on MHI2 units with pc binary

[ -z "$GEM" ] && echo -ne "\033[36m"
echo -ne "CarPlay and Android Auto Status\n\n" | $TEE -a $LOG
[ -z "$GEM" ] && echo -ne "\033[m"
echo -ne "\nCarplay: " | $TEE -a $LOG
$PC b:0:3221356628:8.0 | $TEE -a $LOG
echo -ne "\nAndroid Auto: " | $TEE -a $LOG
$PC b:0:3221356628:7.7 | $TEE -a $LOG
echo -ne "\nUSB Status: " | $TEE -a $LOG
$PC i:0:1343769792 | $TEE -a $LOG
	
return 2> /dev/null

	};;	
	

	# help or unknown parameter ------------------------------
	*) {
		echo ""
		echo $revision
		echo ""
		echo "Usage: "$thisname" [OPTION1] [-noboot]"
		echo ""
		echo "Options 1:"
		echo "        -a 			activated"
		echo "        -s 			status"
		echo "        --help			show this help"
		echo ""
		echo "Option 2:"
		echo "        -noboot	no reboot triggered by script"
		echo ""
		echo "This program is free software; you can redistribute it and/or"
		echo "modify it under the terms of the GNU General Public License"
		echo "as published by the Free Software Foundation; either version 2"
		echo "of the License, or (at your option) any later version."
		echo ""
		echo "This program is distributed in the hope that it will be useful,"
		echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
		echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
		echo "See the GNU General Public License for more details."
		echo ""
		echo "You should have received a copy of the GNU General Public License"
		echo "along with this program; if not, write to the Free Software Foundation,"
		echo "Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA."
		echo ""

	};;

	esac

exit 0
